package arithmetic.LeetCode;

import java.util.Arrays;

import org.junit.jupiter.api.Test;

/**
 * @author jiangfeng on 2022/9/23
 */
public class RemoveEle {

    @Test
    public void test() {
        int[] ints = {3, 2, 2, 3};
        //        int i =

        //        int[] ints = {1};
        int i = removeElement2(ints, 3);
        System.out.println(i);
        System.out.println(Arrays.toString(ints));
    }

    public int removeElement(int[] nums, int val) {
        int l = 0, r = nums.length - 1, temp = 0, result = nums.length;


        while (l <= r) {
            if (nums[l] == val) {
                while (nums[r] == val) {
                    result--;
                    if (r-- == l) {
                        return result;
                    }
                }

                temp = nums[r];
                nums[r] = nums[l];
                nums[l] = temp;
                r--;
                result--;
            }
            l++;
        }
        return result;

    }

    public int removeElement2(int[] nums, int val) {
        int l = 0, r = nums.length - 1;
        while (l <= r) {
            while (l <= r && nums[r] == val) {
                r--;
            }
            if (l > r) {
                return l;
            }
            if (nums[l] == val) {
                nums[l] = nums[r];
                r--;
            }
            l++;
        }
        return l;

    }
}

